/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package spring;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import phys.State;
import phys.TimeLoop;
import vec.Vector3f;

/**
 *
 * @author Vic
 */
public class Gravity extends Element {

    float mass;
    float dampening;
    final int CIRCLES = 5;
    TimeLoop time;
    public Gravity(float mass, float dampening) {
        this.state = new State();
        this.mass = mass;
        this.dampening = dampening;
        time=new TimeLoop(1,2);
    }

    @Override
    public Vector3f getAppliedForce(Element acc, float t) {
        Vector3f displacement = (Vector3f) ((Vector3f) acc.getPosition().clone()).sub(getPosition());
        float r = displacement.length()+1;
        if (r > 50) {
            displacement.scale(-mass / (r * r * r));
        } else if (r > 30) {
            displacement.scale(-mass / (r * r * r*2));
        } else if (r > 20) {
            displacement.scale(-mass / (r * r * r*10));
        } else {
            displacement.scale(-mass / 200000);
        }

        displacement.add(((Vector3f) acc.getVelocity().clone()).scale(dampening));
        //System.out.println("displacement = " + displacement);
        return displacement;
    }

    @Override
    public void draw(Graphics g, Vector3f origen) {
        for (int i = 0; i < CIRCLES; i++) {
            int r = 10 * i + 20;
            r -= Math.signum(mass) *20*time.getTime();
//            g.setColor(new Color(0,0,0,50*i));
//            System.out.println("t = " + t);
            g.drawOval((int) (getPosition().x - r), (int) (2 * origen.y - getPosition().y - r), 2 * r, 2 * r);
        }
        g.setColor(Color.BLACK);
        g.fillOval((int) (getPosition().x - 10), (int) (2 * origen.y - getPosition().y - 10), 20, 20);
    }

    @Override
    public boolean collides(Vector3f point) {
        boolean collides;
        double dist = (StrictMath.hypot(point.x - getPosition().x, point.y - getPosition().y));
        collides = dist < 20;
        return collides;
    }

    @Override
    public void setState(State s) {
    }



    @Override
    public Vector3f getAcceleration(State state, float t) {
        return new Vector3f();
    }

    @Override
    public Rectangle getBounds() {
        return new Rectangle();
    }

    @Override
    public Vector3f getMaxInDir(Vector3f dir) {
        return new Vector3f();
    }

    @Override
    public boolean isPassive() {
        return true;
    }
}
